/*
 * 文件名：BussinessService.java 版权：Copyright by www.ixuenong.com 描述：
 */

package com.pointlion.sys.mvc.admin.business;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.pointlion.sys.mvc.admin.workflow.WorkFlowService;
import com.pointlion.sys.mvc.common.model.OaBusiness;
import com.pointlion.sys.mvc.common.model.VTasklist;
import com.pointlion.sys.plugin.shiro.ShiroKit;

public class BusinessService
{

    public static final BusinessService me = new BusinessService();

    /***
     * 启动流程
     */
    @Before(Tx.class)
    public void startProcess(String id)
    {
        WorkFlowService service = new WorkFlowService();
        String procInsId = service.startProcess(id, BusinessConstants.DEFKEY_BUSINESS, null);
        OaBusiness o = OaBusiness.dao.findById(id);
        o.setProcInsId(procInsId);
        // 审核中
        o.setState(1);
        o.setCreateTime(new Date());
        o.update();
        // 执行第一个任务
        String taskId = service.getTaskIdFromProInsIdAndAssignId(procInsId,
            ShiroKit.getUsername());
        if (StrKit.notBlank(taskId))
        {
            service.completeTask(taskId, "", new HashMap<String,Object>() {
                private static final long serialVersionUID = 1L;

            {
                put("author",o.getAuthor1());
            }});
        }
    }

    /***
     * 提交任务
     */
    @Before(Tx.class)
    public void completeTask(boolean isPass, String comment, String taskid, OaBusiness business)
    {
        WorkFlowService service = new WorkFlowService();
        Map<String, Object> var = new HashMap<String, Object>();
        var.put("pass", isPass);
        if (!isPass)
        {
            // 驳回
            business.setState(3);
        }
        else
        {
            VTasklist task = VTasklist.dao.findById(taskid);
            if (task.getTASKDEFKEY().equals("sujectReview"))
            {
                var.put("author", Db.queryStr("select author2 from oa_business where id=?",business.getId()));
            }
            else if (task.getTASKDEFKEY().equals("CEO"))
            {
                //已完成
                business.setState(2);
            }
        }
        service.completeTask(taskid, comment, var);// 提交任务
        business.update();
    }

    /***
     * 
     */
    public Page<Record> getPartList(int curr, int pageSize, String title, String username)
    {
        String sql = " FROM oa_business b where  exists (select 1 from act_hi_taskinst t where t.proc_inst_id_ = b.proc_ins_id and t.ASSIGNEE_ = '%s') and state =2";
        String username1 = ShiroKit.getUsername();// 用户主键
        sql = String.format(sql, username1);
        Page<Record> page = Db.paginate(curr, pageSize, "SELECT DISTINCT b.* ", sql);
        return page;
    }
    /***
	 * 根据主键查询
	 */
	public OaBusiness getById(String id){
		return OaBusiness.dao.findById(id);
	}
    /***
	 * 删除
	 * @param ids
	 */
	@Before(Tx.class)
	public void deleteByIds(String ids){
    	String idarr[] = ids.split(",");
    	for(String id : idarr){
    		OaBusiness o = me.getById(id);
    		o.delete();
    	}
	}

}
